package com.jackie.tongzhuang.JinHuoDan;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.jackie.tongzhuang.Adapters.JinHuoDanSpinnerAdapter;
import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.Controls.FenQiList;
import com.jackie.tongzhuang.Controls.TitleBar;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Models.FenQi;
import com.jackie.tongzhuang.Models.JinHuoDan;
import com.jackie.tongzhuang.R;

import java.util.ArrayList;
import java.util.List;

public class JinHuoDanFenQiActivity extends AppCompatActivity {

    private Spinner jhdspinner;
    private LinearLayout jhdfqlist;
    private TitleBar titleBar;
    private TextView yhketext,whketext;
    private RadioButton allradio,whqradio;
    private DbClass dbClass = new DbClass(this);
    private List<JinHuoDan> jhds = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fenqi_jinhuodan);
        initView();

        allradio.setChecked(true);  //未还清
        jhds = dbClass.getJinHuoDan("whq");
        initSpinner(jhds);






        titleBar.setOnClickedListener(new TitleBar.TitleBarClickListener() {
            @Override
            public void backClick(View v) {
                finish();
            }

            @Override
            public void submitClick(View v) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        allradio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                jhds = dbClass.getJinHuoDan("all");
                initSpinner(jhds);
                Log.v("msgg","all="+jhds.size());
            }
        });
        whqradio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                jhds = dbClass.getJinHuoDan("whq");
                initSpinner(jhds);
                Log.v("msgg","whq="+jhds.size());
            }
        });
    }

    private void initSpinner(List<JinHuoDan> jhds){
        JinHuoDanSpinnerAdapter jhdAdapter = new JinHuoDanSpinnerAdapter(JinHuoDanFenQiActivity.this,jhds);

        jhdspinner.setAdapter(jhdAdapter);
        jhdspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                jhdfqlist.removeAllViews();
                //获取此进货单的分期信息
                JinHuoDan jhd = (JinHuoDan)adapterView.getSelectedItem();

                yhketext.setText("¥ "+BaseClass.getDecimalToStr(2,dbClass.getHuanKuanEByJinHuoDanId(String.valueOf(jhd.getId()),1)));
                whketext.setText("¥ "+BaseClass.getDecimalToStr(2,dbClass.getHuanKuanEByJinHuoDanId(String.valueOf(jhd.getId()),0)));

                List<FenQi> fqs = dbClass.getFenQiByJinHuoDanId(String.valueOf(jhd.getId()));
                for(FenQi fq : fqs){
                    FenQiList fql = new FenQiList(JinHuoDanFenQiActivity.this,null);
                    fql.setData(fq);
                    fql.setYiChangHuan(fq.getYiChangHuan() == 0 ? false : true);
                    fql.setShowcheck(false);
                    jhdfqlist.addView(fql);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initView(){
        jhdspinner = findViewById(R.id.jhd_fq_jhdspinner);
        jhdfqlist = findViewById(R.id.jhd_fq_fqlist);
        yhketext = findViewById(R.id.jhd_fq_yhke);
        whketext = findViewById(R.id.jhd_fq_whke);
        titleBar = findViewById(R.id.jhd_fq_titlebar);
        whqradio = findViewById(R.id.jhd_fq_whq);
        allradio = findViewById(R.id.jhd_fq_all);
    }
}